﻿namespace DotRas
{
    using pptpclient.Properties;
    using System;
    using System.Collections.ObjectModel;
    using System.Net;
    using System.Net.Sockets;
    using System.Runtime.InteropServices;
    using System.Security.Permissions;
    using System.Text;

    internal static class Utilities
    {
        public static string BuildStringList(Collection<string> collection, char separatorChar, out int length)
        {
            StringBuilder builder = new StringBuilder();
            if ((collection != null) && (collection.Count > 0))
            {
                foreach (string str in collection)
                {
                    if (!string.IsNullOrEmpty(str))
                    {
                        builder.Append(str).Append(separatorChar);
                    }
                }
                builder.Append(separatorChar);
            }
            length = builder.Length * 2;
            return builder.ToString();
        }

        [SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.UnmanagedCode)]
        public static IntPtr CopyObjectsToNewPtr<T>(T[] array, ref int size, out int totalSize)
        {
            if (array == null)
            {
                DotRas.ThrowHelper.ThrowArgumentNullException("array");
            }
            if (size == 0)
            {
                size = Marshal.SizeOf(typeof(T));
            }
            totalSize = array.Length * size;
            IntPtr ptr = Marshal.AllocHGlobal(totalSize);
            CopyObjectsToPtr<T>(array, ptr, ref size);
            return ptr;
        }

        public static void CopyObjectsToPtr<T>(T[] array, IntPtr ptr, ref int size)
        {
            if (array == null)
            {
                DotRas.ThrowHelper.ThrowArgumentNullException("array");
            }
            if (size == 0)
            {
                size = Marshal.SizeOf(typeof(T));
            }
            for (int i = 0; i < array.Length; i++)
            {
                IntPtr ptr2 = new IntPtr(ptr.ToInt64() + (i * size));
                Marshal.StructureToPtr(array[i], ptr2, true);
            }
        }

        [SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.UnmanagedCode)]
        public static void CopyString(IntPtr ptr, int offset, string value, int length)
        {
            if (value == null)
            {
                DotRas.ThrowHelper.ThrowArgumentNullException("value");
            }
            if (length > value.Length)
            {
                DotRas.ThrowHelper.ThrowArgumentException("length", Resources.Argument_ValueLongerThanString);
            }
            IntPtr destination = new IntPtr(ptr.ToInt64() + offset);
            IntPtr zero = IntPtr.Zero;
            try
            {
                zero = Marshal.StringToHGlobalUni(value);
                if (zero != IntPtr.Zero)
                {
                    DotRas.UnsafeNativeMethods.CopyMemory(destination, zero, new IntPtr(length));
                }
            }
            finally
            {
                if (zero != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(zero);
                }
            }
        }

        [SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.UnmanagedCode)]
        public static T[] CreateArrayOfType<T>(IntPtr ptr, int size, int count)
        {
            T[] localArray = new T[count];
            for (int i = 0; i < count; i++)
            {
                IntPtr ptr2 = new IntPtr(ptr.ToInt64() + (i * size));
                localArray[i] = (T) Marshal.PtrToStructure(ptr2, typeof(T));
            }
            return localArray;
        }

        [SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.UnmanagedCode)]
        public static Collection<string> CreateStringCollection(IntPtr ptr, int offset, int count)
        {
            Collection<string> collection = new Collection<string>();
            IntPtr ptr2 = new IntPtr(ptr.ToInt64() + offset);
            bool flag = false;
            int num = 0;
            do
            {
                string str = Marshal.PtrToStringUni(ptr2);
                if (string.IsNullOrEmpty(str))
                {
                    return collection;
                }
                collection.Add(str);
                ptr2 = new IntPtr((ptr2.ToInt64() + (str.Length * 2)) + 2L);
                num++;
            }
            while (((count != 0) && (num < count)) && !flag);
            return collection;
        }

        public static DotRas.NativeMethods.RASIPADDR GetRasIPAddress(IPAddress value)
        {
            DotRas.NativeMethods.RASIPADDR rasipaddr = new DotRas.NativeMethods.RASIPADDR();
            if (value != null)
            {
                if (value.AddressFamily != AddressFamily.InterNetwork)
                {
                    DotRas.ThrowHelper.ThrowArgumentException("value", Resources.Argument_IncorrectAddressFamily);
                }
                if (value == null)
                {
                    rasipaddr.addr = IPAddress.Any.GetAddressBytes();
                    return rasipaddr;
                }
                rasipaddr.addr = value.GetAddressBytes();
            }
            return rasipaddr;
        }

        public static bool IsHandleInvalidOrClosed(RasHandle handle)
        {
            if ((handle != null) && !handle.IsInvalid)
            {
                return handle.IsClosed;
            }
            return true;
        }
    }
}

